import axios from "axios";

//引入ElementUI中的全局提示方法
import { Message } from "element-ui"
// console.log(Message)
// console.log(MessageBox)
import nprogress from "nprogress"
let http = axios.create({
    withCredentials: true,
    baseURL: "/api",
    timeout: 5000
})

//请求拦截
http.interceptors.request.use(config => {
    nprogress.start()
    let token = localStorage.getItem('w-token')
    config.headers.authorization = token
    return config
})

//响应拦截
http.interceptors.response.use(config => {
    nprogress.done()
    let { data: { state, msg, code } } = config
    //连接器判断后端响应的state
    //10022登录过期
    //1004 token过期
    //10026 没有权限
    let codes = ['10022', '1004', '10026']
    if (codes.includes(code)) {
        //清除token
        localStorage.removeItem('w-token')
        //刷新页面
        location.reload()
    }


    //一个需要排除的不弹窗的接口

    let excludeUrl = ["/order/query_order_status"]

    //判断不需要弹窗的数组中是否包含当前的请求的这个路径
    let isExclude = excludeUrl.includes(config.config.url)

    if (!state && !isExclude) {
        //全局提示
        // MessageBox.alert(msg,'错误提示',{type:"error"})
        Message.error(msg)
    }

    return config
})


export default http;